package com.handinfo.android.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;

public class Tools
{
	public final static boolean DEBUG = true; //此为假时,混淆后会删除调试信息以减小JAR包
	public final static boolean DEBUG2 = true; //此为假时,混淆后会删除调试信息以减小JAR包
	public static final String DEBUG_STRING = "调试信息";

//	public static final Short EDITION_DEBUG = -1;//内部测试
//	public static final Short CHANNEL = EDITION_DEBUG;

	//******************************我的代码*********************************//

	// ///////////////////////////////////////////////////////////
	// 颜色区域
	// ///////////////////////////////////////////////////////////
	/** 红色 */
	public static final int RED = 0xFFFF0000;
	/** 黄色 */
	public static final int YELLOW = 0xFFFFFF00;
	/** 紫色 */
	public static final int PURPLE = 0xFFFF00FF;
	/** 黑色 */
	public static final int BLACK = 0xFF000000;
	/** 蓝色 */
	public static final int BLUE = 0xFF0000FF;
	/** 青色 */
	public static final int CYAN = 0xFF000080;
	/** 深灰色 */
	public static final int DARK_GRAY = 0xFFA9A9A9;
	/** 灰色 */
	public static final int GRAY = 0xFF808080;
	/** 绿色 */
	public static final int GREEN = 0xFF00FF00;
	/** 浅灰色 */
	public static final int LIGHT_GRAY = 0xFFD3D3D3;
	/** 洋红色 */
	public static final int MAGENTA = 0xFFFF00FF;
	/** 粉红色 */
	public static final int PINK = 0xFFFFC0CB;
	/** 白色 */
	public static final int WHITE = 0xFFFFFFFF;
	/** 橙色 */
	public static final int ORANGE = 0xFFFF9900;
	/** 透明色 */
	public static final int ALPHA = 0x00123456;

	public static final int DEEP_YELLOW = 0xFFFFBB11;

	public static final int LILIANG = 0xFFd36060;

	public static final int ZHILI = 0xFFddb956;

	public static final int JIQIAO = 0xFF6065d3;

	public static final int MINJIE = 0xFF60d3d3;

	public static final int JIAZU = 0xFF7777FF;

	// 对齐方式
	public static final int BOTTOM = DWGraphics.BOTTOM;

	public static final int HCENTER = DWGraphics.HCENTER;

	public static final int LEFT = DWGraphics.LEFT;

	public static final int RIGHT = DWGraphics.RIGHT;

	public static final int TOP = DWGraphics.TOP;

	public static final int VCENTER = DWGraphics.VCENTER;

	public static final int LEFT_TOP = LEFT | TOP;

	public static final int LEFT_VCENTER = LEFT | VCENTER;

	public static final int LEFT_BOTTOM = LEFT | BOTTOM;

	public static final int HCENTER_TOP = HCENTER | TOP;

	public static final int HCENTER_VCENTER = HCENTER | VCENTER;

	public static final int HCENTER_BOTTOM = HCENTER | BOTTOM;

	public static final int RIGHT_TOP = RIGHT | TOP;

	public static final int RIGHT_VCENTER = RIGHT | VCENTER;

	public static final int RIGHT_BOTTOM = RIGHT | BOTTOM;

	public static boolean collide(int x, int y, int x2, int y2, int oX, int oY,
			int oX2, int oY2)
	{
		return ((x < oX2) && (x2 > oX) && (y < oY2) && (y2 > oY));
	}

	public static boolean collide(float x, float y, float x2, float y2, float oX, float oY,
			float oX2, float oY2)
	{
		return ((x < oX2) && (x2 > oX) && (y < oY2) && (y2 > oY));
	}

	public static void debugPrintln(String msg)
	{
		if (DEBUG) {
			Log.d(DEBUG_STRING, msg);
		}
	}

	public static void debugPrintln(String filter, String msg)
	{
		if (DEBUG) {
			Log.d(filter, msg);
		}
	}

	public static final void debug(Exception ex)
	{
		if (DEBUG2) {
			ex.printStackTrace();
		}
		else {
			debugPrintln(ex.toString());
		}
	}

	/**
	 * 分割字符串
	 * 
	 * @param string
	 * @param regex
	 * @return
	 */
	public static String[] splitString(String string, char regex)
	{
		if (string == null)
			return null;
		if (string.length() == 0)
			return new String[] { string };
		int start = 0, end = 0;
		ArrayList<String> objs = new ArrayList<String>();
		while (end < string.length()) {
			while (end < string.length() && string.charAt(end) != regex) {
				end++;
			}
			String tmp = string.substring(start, end);
			objs.add(tmp);
			if (end < string.length() && (string.charAt(end) == regex)) {
				end++;
			}
			start = end;
		}

		String[] strings = new String[objs.size()];
		for (int i = 0; i < strings.length; i++) {
			strings[i] = (String) objs.get(i);
		}
		return strings;
	}

	/**
	 * 将指定的字符串指定的字符分割为字符串数组
	 * 
	 * @param inf
	 * @param tag 标记字符
	 * @param width 宽度限制，到达宽度则换行
	 * @return
	 */
	public static String[] splitString(String inf, DWFont font, char tag, int width, int x)
	{
		if (inf == null) {
			return null;
		}
		int start = 0, end = 0, length = x;
		String str = inf;
		int tmp = 1;
		// 计算出分割后数组大小
		while (end < str.length()) {
			if (str.charAt(end) == tag
					|| str.charAt(end) == '\n'
					|| (width != 0 && length + font.charWidth(str.charAt(end)) >= width)) {
				tmp++;
				length = 0;
			}
			length += font.charWidth(str.charAt(end));
			end++;
		}
		String[] s_control = new String[tmp];
		s_control[0] = "";
		tmp = 0;
		end = 0;
		length = 0;
		// 遍历所有字符来进行分割
		while (end < str.length()) {
			while (end < str.length()
					&& str.charAt(end) != tag
					&& str.charAt(end) != '\n'
					&& (width == 0 || (width != 0 && length
							+ font.charWidth(str.charAt(end)) < width))) {
				length += font.charWidth(str.charAt(end));
				end++;
			}
			s_control[tmp] = str.substring(start, end);
			tmp++;
			length = 0;
			if (end < str.length()
					&& (str.charAt(end) == tag || str.charAt(end) == '\n')) {
				end++;
			}
			start = end;
		}
		while (s_control[s_control.length - 1] == null) {
			String[] s_tmp = new String[s_control.length - 1];
			arraycopy(s_control, 0, s_tmp, 0, s_tmp.length);
			s_control = null;
			s_control = s_tmp;
		}
		return s_control;
	}

	/**
	 * 数组复制
	 * 
	 * @param object
	 * @param _int
	 * @param object2
	 * @param _int3
	 * @param _int4
	 */
	public static void arraycopy(Object object, int _int, Object object2,
			int _int3, int _int4)
	{
		System.arraycopy(object, _int, object2, _int3, _int4);
	}

	/**
	 * d 根据宽度拆分字符
	 * 
	 * @param text
	 * @param w
	 * @return
	 */
	public static final String[] splitString(String text, int w, int x, DWFont font)
	{
		ArrayList<String> texts = new ArrayList<String>();
		String temp = "";
		int amountW = 0, curW = x;
		for (int i = 0; i < text.length(); i++) {
			if (i == text.length() - 1) {
				texts.add(temp + text.substring(i, text.length()));
				break;
			}
			else {
				amountW = (int) font.stringWidth(temp);
				curW = (int) font.stringWidth(text.substring(i, i + 1));
				if (amountW + curW > w) {
					texts.add(temp);
					temp = text.substring(i, i + 1);
				}
				else {
					temp += text.substring(i, i + 1);
				}
			}
		}
		String[] list = new String[texts.size()];
		for (int i = 0; i < list.length; i++) {
			list[i] = texts.get(i).toString();
		}
		return list;
	}

	/**
	 * 
	 * 
	 * @param num
	 * @param min
	 * @param max
	 * @return
	 */
	public static int limit(int num, int min, int max)
	{
		if (min > max) {
			int tmp = max;
			max = min;
			min = tmp;
		}
		if (num > max) {
			return max;
		}
		else if (num < min) {
			return min;
		}
		else {
			return num;
		}
	}

	public static int pow2(int size)
	{
		int small = (int) (Math.log((double) size) / Math.log(2.0f));
		if ((1 << small) >= size)
			return 1 << small;
		else
			return 1 << (small + 1);
	}

	/**
	 * 获得二进制中第X位，1返回true ，2 返回false 2012-2-24 上午08:16:48 venizeng
	 * @param num
	 * @param index
	 * @return
	 */
	public static final boolean getBitNum(long num, int index)
	{
		for (int i = 0; i < index; i++) {
			num >>= 1;
		}
		return (num & 0x01) == 1;
	}

	/**
	 * 改变数组大小（byte）
	 * 
	 * @param array
	 * @param index
	 * @param var
	 */
	public static final byte[] resizeArray(byte[] array, int index, int var)
	{
		int len = array.length;
		byte[] tmp = array;
		array = new byte[len + var];
		System.arraycopy(tmp, 0, array, 0, index);
		if (var > 0) {
			System.arraycopy(tmp, index, array, index + var, len - index);
		}
		else {
			System.arraycopy(tmp, index - var, array, index, len + var - index);
		}
		tmp = null;
		return array;
	}

	/**
	 * xu 是否在圆内
	 * @param x1 当前点x
	 * @param x2
	 * @param cirX 圆心x
	 * @param cirY
	 * @param r 半径
	 * @return
	 */
	public static final boolean isInCircle(float x1, float y1, float cirX, float cirY, float r)
	{
		double distance = distancePointToPoint(x1, y1, cirX, cirY);
		if (distance < r) {
			return true;
		}
		return false;
	}

	/**
	 * xu 是否在矩形内
	 * @param x1 点击的 x
	 * @param y1
	 * @param recX 矩形的左上角点
	 * @param recY
	 * @param w
	 * @param h
	 * @return
	 */
	public static final boolean isInRectangle(float x1, float y1, float cirX, float cirY, float w, float h)
	{
		if (((x1 - cirX) >= 0) && ((x1 - cirX) <= w) && ((y1 - cirY) >= 0) && ((y1 - cirY) <= h)) {
			return true;
		}
		return false;
	}

	/**
	 * 点是否在矩形内
	 * @param x
	 * @param y
	 * @param rect 0:left 1:up 2:right 3:down
	 * @return
	 */
	public static boolean pointInRectangle(float x, float y, float rx1, float ry1, float rx2, float ry2)
	{
		if (x >= rx1 && x <= rx2 && y >= ry1 && y <= ry2)
			return true;
		else
			return false;
	}

	/**
	 * 两点间距离
	 * 
	 * @param x1 int
	 * @param y1 int
	 * @param x2 int
	 * @param y2 int
	 * @return int
	 */
	public static double distancePointToPoint(float x1, float y1, float x2, float y2)
	{

		return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
	}

	/**
	 * 两点间距离的平方
	 * 
	 * @param x1 int
	 * @param y1 int
	 * @param x2 int
	 * @param y2 int
	 * @return int
	 */
	public static float distanceSquare(float x1, float y1, float x2, float y2)
	{
		return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
	}

	private static Random rand = new Random(System.currentTimeMillis());

	/**
	 * 返回给定范围内的一个随机数值
	 * 
	 * @param min
	 * @param max
	 * @return
	 */
	public static int random(int min, int max)
	{
		if (max < min) {
			int tmp = max;
			max = min;
			min = tmp;
		}
		int num = Math.abs(rand.nextInt()) % (max - min + 1);
		return num + min;
	}

	/**
	 * 返回不小于0且不大于指定数的一个随机数值
	 * 
	 * @param max int 指定数
	 * @return
	 */
	public static int random(int max)
	{
		return Math.abs(rand.nextInt()) % (max + 1);
	}

//	public static Bitmap createBinImage(String path)
//	{
//		if (path == null) {
//			return null;
//		}
////		InputStream is = path.getClass().getResourceAsStream(path);
//		InputStream is = ResourceManager.getInstance().openStream(path);
//		return createBinImage(is);
//	}
	public static InputStream getResourceAndroidRes(Context context, int id)
	{
		InputStream is = context.getResources().openRawResource(id);
		return is;
	}

	public static Bitmap createBinImage(InputStream is)
	{
		if (is == null) {
			return null;
		}
		byte[] data = null;
		int startIndex = 0;//其实位置
		int len = 0;//长度
		try {
			int size = is.available();
			data = new byte[size + 20];
			//
			is.read(data, 8, size);
			if (data[8] == (byte) 0x89) {// 如果是头文件信息，肯定是服务器数据
				startIndex = 8;
				len = size;
			}
			else {//如果是客户端文件
				startIndex = 0;
				len = size + 20;
				//
				data[0] = (byte) 0x89;
				data[1] = 0x50;
				data[2] = 0x4e;
				data[3] = 0x47;
				data[4] = 0x0d;
				data[5] = 0x0A;
				data[6] = 0x1a;
				data[7] = 0x0a;
				//
				data[len - 1] = (byte) 0x82;
				data[len - 2] = 0x60;
				data[len - 3] = 0x42;
				data[len - 4] = (byte) 0xae;
				data[len - 5] = 0x44;
				data[len - 6] = 0x4e;
				data[len - 7] = 0x45;
				data[len - 8] = 0x49;
				data[len - 9] = 0x00;
				data[len - 10] = 0x00;
				data[len - 11] = 0x00;
				data[len - 12] = 0x00;
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		finally {
			try {
				is.close();
			}
			catch (IOException e) {
				showErrorMessage("CreatImage error!|path = ", e);
			}
		}
		if (data != null) {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inInputShareable = true;
			opt.inPurgeable = true;//设置图片可以被回收
			opt.inTempStorage = new byte[12 * 1024];
			return BitmapFactory.decodeByteArray(data, startIndex, len, opt);
//			return BitmapFactory.decodeByteArray(data, startIndex, len);
		}
		return null;
	}

	public static void showErrorMessage(String message, Exception e)
	{

	}

	public static final int NUM_SIGN_PLUS = 10;
	public static final int NUM_SIGN_MINUS = 11;

	public static void drawNumber(DWGraphics g, Bitmap img, int x, int y, int w,
			int h, long number, int interval, int anchor, boolean sign)
	{
		float clipX = g.getClipX();
		float clipY = g.getClipY();
		float width = g.getClipWidth();
		float height = g.getClipHeight();
		long num = Math.abs(number);
		byte[] nums = new byte[20];
		int digit = 0;
		for (int i = 0; i < 20; i++) {
			long m = num % 10;
			num /= 10;
			nums[i] = (byte) m;
			if (num == 0) {
				digit = i + 1;
				break;
			}
		}
		int sx = x, sy = y;
		switch (anchor) {
			case DWGraphics.LEFT | DWGraphics.BOTTOM:
				sy = y - h;
				break;
			case DWGraphics.LEFT | DWGraphics.TOP:
				break;
			case DWGraphics.RIGHT | DWGraphics.BOTTOM:
				sx = x - digit * (w + interval);
				sy = y - h;
				break;
			case DWGraphics.RIGHT | DWGraphics.TOP:
				sx = x - digit * (w + interval);
				break;
			case DWGraphics.BOTTOM | DWGraphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h;
				break;
			case DWGraphics.TOP | DWGraphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				break;
			case DWGraphics.HCENTER | DWGraphics.VCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h / 2;
				break;
		}

		for (int i = 0; i < digit; i++) {
			g.setClip(sx + (digit - i - 1) * (w + interval), sy,
					(w + interval), h);
			g.drawBitmap(img, sx - nums[i] * w + (digit - i - 1)
					* (w + interval), sy, DWGraphics.LEFT | DWGraphics.TOP);
		}

		if (sign) {
			g.setClip(sx - (w + interval), sy, (w + interval), h);
			if (number > 0) {
				g.drawBitmap(img, sx - NUM_SIGN_PLUS * w - (w + interval), sy,
						DWGraphics.LEFT | DWGraphics.TOP);
			}
			else if (number < 0) {
				g.drawBitmap(img, sx - NUM_SIGN_MINUS * w - (w + interval), sy,
						DWGraphics.LEFT | DWGraphics.TOP);
			}
		}
		g.setClip(clipX, clipY, width, height);
	}
	
	public static int getNumberWidth(long number){
		long num = Math.abs(number);
		byte[] nums = new byte[20];
		int digit = 0;
		for (int i = 0; i < 20; i++) {
			long m = num % 10;
			num /= 10;
			nums[i] = (byte) m;
			if (num == 0) {
				digit = i + 1;
				break;
			}
		}
		return digit;
	}

	public static void drawNumber(DWGraphics g, Bitmap img, int x, int y, int srcw,
			int srch, float scaleX, float scaleY, long number, int src_interval, int anchor, boolean sign)
	{
		float bitmapWidth = img.getWidth() * scaleX;
		float bitmapHeight = img.getHeight() * scaleY;
		float clipX = g.getClipX();
		float clipY = g.getClipY();
		float width = g.getClipWidth();
		float height = g.getClipHeight();
		long num = Math.abs(number);
		byte[] nums = new byte[20];
		int digit = 0;
		for (int i = 0; i < 20; i++) {
			long m = num % 10;
			num /= 10;
			nums[i] = (byte) m;
			if (num == 0) {
				digit = i + 1;
				break;
			}
		}
		float sx = x, sy = y;
		float w = srcw * scaleX;
		float h = srch * scaleY;
		float interval = src_interval * scaleX;
		switch (anchor) {
			case DWGraphics.LEFT | DWGraphics.BOTTOM:
				sy = y - h;
				break;
			case DWGraphics.LEFT | DWGraphics.TOP:
				break;
			case DWGraphics.RIGHT | DWGraphics.BOTTOM:
				sx = x - digit * (w + interval);
				sy = y - h;
				break;
			case DWGraphics.RIGHT | DWGraphics.TOP:
				sx = x - digit * (w + interval);
				break;
			case DWGraphics.BOTTOM | DWGraphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h;
				break;
			case DWGraphics.TOP | DWGraphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				break;
			case DWGraphics.HCENTER | DWGraphics.VCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h / 2;
				break;
		}

		for (int i = 0; i < digit; i++) {
			g.setClip(sx + (digit - i - 1) * (w + interval), sy,
					(w + interval), h);
			g.drawBitmap(img, sx - nums[i] * w + (digit - i - 1)
					* (w + interval), sy, bitmapWidth, bitmapHeight, DWGraphics.LEFT | DWGraphics.TOP);
		}

		if (sign) {
			g.setClip(sx - (w + interval), sy, (w + interval), h);
			if (number > 0) {
				g.drawBitmap(img, sx - NUM_SIGN_PLUS * w - (w + interval), sy,
						bitmapWidth, bitmapHeight, DWGraphics.LEFT | DWGraphics.TOP);
			}
			else if (number < 0) {
				g.drawBitmap(img, sx - NUM_SIGN_MINUS * w - (w + interval), sy,
						bitmapWidth, bitmapHeight, DWGraphics.LEFT | DWGraphics.TOP);
			}
		}
		g.setClip(clipX, clipY, width, height);
	}

	public static void drawNumber(DWGraphics g, Bitmap img, int x, int y, int w,
			int h, long number, int interval, int anchor, Bitmap extra)
	{
		float clipX = g.getClipX();
		float clipY = g.getClipY();
		float width = g.getClipWidth();
		float height = g.getClipHeight();
		long num = Math.abs(number);
		byte[] nums = new byte[20];
		int digit = 0;
		for (int i = 0; i < 20; i++) {
			long m = num % 10;
			num /= 10;
			nums[i] = (byte) m;
			if (num == 0) {
				digit = i + 1;
				break;
			}
		}
		int sx = x, sy = y;
		switch (anchor) {
			case DWGraphics.LEFT | DWGraphics.BOTTOM:
				sy = y - h;
				break;
			case DWGraphics.LEFT | DWGraphics.TOP:
				break;
			case DWGraphics.RIGHT | DWGraphics.BOTTOM:
				sx = x - digit * (w + interval);
				sy = y - h;
				break;
			case DWGraphics.RIGHT | DWGraphics.TOP:
				sx = x - digit * (w + interval);
				break;
			case DWGraphics.BOTTOM | DWGraphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h;
				break;
			case DWGraphics.TOP | DWGraphics.HCENTER:
				sx = x - digit * (w + interval) / 2;
				break;
			case DWGraphics.HCENTER | DWGraphics.VCENTER:
				sx = x - digit * (w + interval) / 2;
				sy = y - h / 2;
				break;
		}

		for (int i = 0; i < digit; i++) {
			g.setClip(sx + (digit - i - 1) * (w + interval), sy,
					(w + interval), h);
			g.drawBitmap(img, sx - nums[i] * w + (digit - i - 1)
					* (w + interval), sy, DWGraphics.LEFT | DWGraphics.TOP);
		}

		if (extra != null) {
			int extraWidth = extra.getWidth();
			int extraHeight = extra.getHeight();
			g.setClip(sx - (extraWidth + interval), sy - (extraHeight - h), (extraWidth + interval), extraHeight);
			g.drawBitmap(extra, sx - interval, sy - (extraHeight - h),
					DWGraphics.RIGHT | DWGraphics.TOP);
		}
		g.setClip(clipX, clipY, width, height);
	}

	public static long getLong(String s)
	{
		long value = 0;
		if (s == null) {
			return value;
		}
		try {
			value = Long.valueOf(s);
		}
		catch (Exception e) {
			return 0;
		}
		return value;
	}

	public static String getMoney(long money)
	{
		String temp = null;
		if (money > 9999 && money < 100000000) {
			temp = String.valueOf(money / 10000).concat("万");
		}
		else if (money >= 100000000) {
			temp = String.valueOf(money / 100000000).concat("亿");
		}
		else {
			temp = String.valueOf(money);
		}
		return temp;
	}

	/**
	 * 对给定数目的自0开始步长为1的数字序列进行乱序
	 * @param no 给定数目
	 * @return 乱序后的数组
	 */
	public static int[] getSequence(int no)
	{
		int[] sequence = new int[no];
		for (int i = 0; i < no; i++) {
			sequence[i] = i;
		}

		for (int i = 0; i < no; i++) {
			int p = Tools.random(no - 1);
			int tmp = sequence[i];

			sequence[i] = sequence[p];
			sequence[p] = tmp;
		}

		return sequence;
	}
}
